<template>
  <n-grid cols="1 s:1 m:1 l:4 xl:4 2xl:4" responsive="screen">
    <n-grid-item>
      <n-form :label-width="80" :model="formParams" :rules="rules" ref="formRef">
        <n-form-item label="账号SecretId" path="sms_secretid">
          <n-input v-model:value="formParams.sms_secretid" placeholder="请输入账号SecretId" />
        </n-form-item>
        <n-form-item label="账号SecretKey" path="sms_secretkey">
          <n-input v-model:value="formParams.sms_secretkey" placeholder="请输入账号SecretKey" />
        </n-form-item>
        <n-form-item label="应用ID" path="sms_smssdkappId">
          <n-input v-model:value="formParams.sms_smssdkappId" placeholder="请输入应用ID" />
        </n-form-item>
        <n-form-item label="模板ID" path="sms_templateid">
          <n-input v-model:value="formParams.sms_templateid" placeholder="请输入模板ID" />
        </n-form-item>
        <n-form-item label="模板签名" path="sms_signname">
          <n-input v-model:value="formParams.sms_signname" placeholder="请输入模板签名" />
        </n-form-item>
        <div>
          <n-space>
            <n-button type="primary" :loading="subLoading" @click="formSubmit">更新短信信息</n-button>
          </n-space>
        </div>
      </n-form>
    </n-grid-item>
  </n-grid>
</template>

<script lang="ts" setup>
  import { ref, onMounted } from 'vue';
  import { useMessage, FormRules } from 'naive-ui';
  import { webconfigEdit,webconfiginfo } from '@/api/webconfig/index';
  
  const rules: FormRules = {
    sms_secretid: {
      required: true,
      message: '请输入账号SecretId',
      trigger: 'blur',
    },sms_secretkey: {
      required: true,
      message: '请输入账号SecretKey',
      trigger: 'input',
    },sms_smssdkappId: {
      required: true,
      message: '请输入应用ID',
      trigger: 'input',
    },sms_templateid: {
      required: true,
      message: '请输入模板ID',
      trigger: 'input',
    },sms_signname: {
      required: true,
      message: '请输入模板签名',
      trigger: 'input',
    },
  };

  const defaultValueRef = () => ({
    sms_secretid: '',
    sms_secretkey: '',
    sms_smssdkappId: '',
    sms_templateid: '',
    sms_signname: '',
  });

  const message = useMessage();
  const formRef: any = ref(null);
  const subLoading = ref(false);
  const formParams = ref(defaultValueRef());

  function formSubmit() {
    formRef.value.validate((errors) => {
      if (!errors) {
        webconfigEdit(formParams.value).then((_) => {
          message.success('更新成功');
        });
      } else {
        message.error('验证失败，请填写完整信息');
      }
    });
  }

  function getInfo() {
    webconfiginfo({}).then((res) => {
      const params = {
        sms_secretid: res.sms_secretid,
        sms_secretkey: res.sms_secretkey,
        sms_smssdkappId: res.sms_smssdkappId,
        sms_templateid: res.sms_templateid,
        sms_signname: res.sms_signname,
      };
      formParams.value = Object.assign(formParams.value, params);
    });
  }

  onMounted(async () => {
    getInfo();
  });
</script>
